package com.zhenyi.pxlm.controller.admin;

import java.sql.SQLException;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.pxlm.common.model.PxlmProduct;
import com.pxlm.common.model.PxlmProductCategory;
import com.pxlm.common.model.PxlmProductFormat;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class PXLMAdminProductController extends PXLMAdminController {

	public void list() {
		Kv kv = getSimpleKv("schoolId", "categoryId");
		SqlPara sqlPara = db.getSqlPara("PXLMFindProducts", kv);
		Page<PxlmProduct> pages = PxlmProduct.dao.paginate(getPage(), getPageSize(), sqlPara);
		setRainbow(pages);
		setData("items", pages.getList());
		render("/pxlm/admin/product/list.html");
	}

	public void info() {
		setReferer();
		Long productId = getParaToLong("productId");
		if (productId != null) {
			PxlmProduct info = PxlmProduct.dao.findById(productId);
			if (info == null) {
				renderFailure(404, "not found");
				return;
			}
			SqlPara spFormats = db.getSqlPara("PXLMFindFormatsByProductId", Kv.by("productId", info.getId()));
			List<Record> formats = db.find(spFormats);
			info.put("formats", formats);
			setData(info);
		}
		SqlPara spCategories = db.getSqlPara("PXLMFindProductCategories");
		List<Record> categories = db.find(spCategories);
		setData("categories", categories);
		render("/pxlm/admin/product/info.html");
	}

	public void submit() {
		Long productId = getParaToLong("productId");
		String title = getPara("title");
		String subtitle = getPara("subtitle");
		String html = getPara("html");
		String cotnent = getPara("cotnent");
		String image = getPara("image");
		String remark = getPara("remark");
		Integer schoolId = getParaToInt("schoolId");
		Long categoryId = getParaToLong("categoryId");
		Integer isHidden = getParaToInt("isHidden", 0);
		Integer sort = getParaToInt("sort", 99);
		String formats = getPara("formats");
		
		boolean flag = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				PxlmProduct info;
				if (productId == null) {
					info = new PxlmProduct();
					info.setSchoolId(schoolId);
					info.setIsHidden(isHidden);
					info.setSort(sort);
					boolean success = info.save();
					if (!success) {
						return false;
					}
				} else {
					info = PxlmProduct.dao.findById(productId);
					if (info == null) {
						renderFailure(404, "not found");
						return false;
					}
				}
				info.setTitle(title);
				info.setSubtitle(subtitle);
				// info.setHtml(html);
				info.setCotnent(cotnent);
				info.setImage(image);
				info.setRemark(remark);
				info.setSchoolId(schoolId);
				info.setCategoryId(categoryId);
				info.setIsHidden(isHidden);
				info.setSort(sort);
				boolean success = info.update();
				if (!success) {
					return false;
				}

				SqlPara spFormats = db.getSqlPara("PXLMFindFormatsByProductId", Kv.by("productId", info.getId()));
				List<PxlmProductFormat> formatList = PxlmProductFormat.dao.find(spFormats);
				for (PxlmProductFormat format : formatList) {
					if (!format.delete()) {
						return false;
					}
				}
				if (JSONUtil.isJson(formats)) {
					JSONArray jsonArray = JSONUtil.parseArray(formats);
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject obj = jsonArray.getJSONObject(i);
						PxlmProductFormat format = new PxlmProductFormat();
						format.setTitle(obj.getStr("title"));
						format.setPrice(obj.getStr("price"));
						Float fee = obj.getFloat("price") * 100;
						format.setFee(fee.intValue());
						format.setImage(obj.getStr("image"));
						format.setSort(i);
						format.setProductId(info.getId());
						if (!format.save()) {
							return false;
						}
					}
				}
				return true;
			}
		});
		renderFlag(flag);
	}

	public void delete() {
		Long productId = getParaToLong("productId");
		if (productId == null) {
			renderFailure("id is null");
			return;
		}
		PxlmProduct info = PxlmProduct.dao.findById(productId);
		if (info == null) {
			renderFailure(404, "not found");
			return;
		}
		renderFlag(info.delete());
	}

	public void categoryList() {
		Kv kv = getSimpleKv("schoolId");
		SqlPara sqlPara = db.getSqlPara("PXLMFindProductCategories", kv);
		Page<PxlmProductCategory> pages = PxlmProductCategory.dao.paginate(getPage(), getPageSize(), sqlPara);
		setRainbow(pages);
		setData("items", pages.getList());
		render("/pxlm/admin/product/categoryList.html");
	}

	public void categoryInfo() {
		setReferer();
		Long categoryId = getParaToLong("categoryId");
		if (categoryId != null) {
			PxlmProductCategory info = PxlmProductCategory.dao.findById(categoryId);
			if (info == null) {
				renderFailure(404, "not found");
				return;
			}
			setData(info);
		}
		render("/pxlm/admin/product/categoryInfo.html");
	}

	public void updateCategory() {
		Long categoryId = getParaToLong("categoryId");
		String title = getPara("title");
		Integer schoolId = getParaToInt("schoolId");
		Integer sort = getParaToInt("sort", 99);
		PxlmProductCategory info;
		if (categoryId == null) {
			info = new PxlmProductCategory();
			info.setSort(sort);
			info.save();
		} else {
			info = PxlmProductCategory.dao.findById(categoryId);
			if (info == null) {
				renderFailure(404, "not found");
				return;
			}
		}
		info.setTitle(title);
		info.setSchoolId(schoolId);
		info.setSort(sort);
		renderFlag(info.update());
	}

	public void deleteCategory() {
		Long categoryId = getParaToLong("categoryId");
		if (categoryId == null) {
			renderFailure("id is null");
			return;
		}
		PxlmProductCategory info = PxlmProductCategory.dao.findById(categoryId);
		if (info == null) {
			renderFailure(404, "not found");
			return;
		}
		renderFlag(info.delete());
	}

}